/**
 * Created with JetBrains WebStorm.
 * User: renji
 * Date: 14/5/13
 * Time: 3:30 PM
 * To change this template use File | Settings | File Templates.
 */

describe("AuthoriseTransactionsController unit test", function(){

    var authTransactionsController = null;

    beforeEach(function(){


        var t_alertCtrl = App.AlertController.create();
        var t_loadingCtrl = App.LoadingController.create();
        App.AuthoriseTransactionsController.reopen({

            init:function(){
                this.set('controllers.alert', t_alertCtrl);
                this.set('controllers.loading', t_loadingCtrl);
            }
        });

        authTransactionsController = App.AuthoriseTransactionsController.create();
        authTransactionsController.listViewData = Ember.Object.create({
            accountVisible: false,
            account: null,
            transactionListByDate: [],
            i18nData: App.i18n.getTranslations()['authorise']
        });
    });
    describe("controller unit test", function(){

        it("prepareController unit test", function(){

            var objE = {errorCode:'code', errorMessage:'message'} ,
                       transferResult = [ ];
            var parameter = { needsRefresh:true };

            spyOn(authTransactionsController, 'authoriseTransactionsCallback');
            spyOn(App.Router,'getParameter').andReturn(parameter);
            spyOn(App.api_authoriseTransaction,'getAuthoriseTransactions').andCallFake(function(callback) {
                callback(transferResult,objE);
            });

            authTransactionsController.prepareController();

            expect(App.api_authoriseTransaction.getAuthoriseTransactions).toHaveBeenCalled();
            expect(authTransactionsController.authoriseTransactionsCallback).toHaveBeenCalled();
            expect(App.Router.getParameter).toHaveBeenCalled();
        });



        it("authoriseTransactionsCallback - Success", function(){

            var objE = {errorCode:'code', errorMessage:'message'};

            spyOn(authTransactionsController, 'hasError').andReturn(false);
            spyOn(authTransactionsController, 'hideLoading');

            authTransactionsController.authoriseTransactionsCallback([], objE);


            expect(authTransactionsController.hasError).toHaveBeenCalled();
            expect(authTransactionsController.hideLoading).toHaveBeenCalled();

        });

        it("authoriseTransactionsCallback - Success with list length > 0", function(){

            var objE = {errorCode:'code', errorMessage:'message'};

            spyOn(authTransactionsController, 'hasError').andReturn(false);
            spyOn(authTransactionsController, 'hideLoading');

            authTransactionsController.authoriseTransactionsCallback([{date:'12-10-2011'},{date:'12-10-2012'}], objE);


            expect(authTransactionsController.hasError).toHaveBeenCalled();
            expect(authTransactionsController.hideLoading).toHaveBeenCalled();

        });

        it("buildTransactionListByDate - Success with list length > 0", function(){

            authTransactionsController.buildTransactionListByDate([{date:'12-10-2011'},{date:'12-10-2012'}]);

        });

        it("selectTransaction", function(){
            var transaction = [ ];
            spyOn(App.getSession(), 'setCurrentTransfer');
            spyOn(authTransactionsController, 'transitionTo');

            authTransactionsController.selectTransaction(transaction);
            expect(App.getSession().setCurrentTransfer).toHaveBeenCalled();
            expect(authTransactionsController.transitionTo).toHaveBeenCalledWith('authorise.transactionDetails',transaction);

        });
    })
});